package application.openGL;

import org.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.Texture;

public class GLGraphics2D 
{
    public void setColor(int color)
    {
	this.setColor((color>>16)&0x0000FF, (color>>8)&0x0000FF, color&0x0000FF);
    }
    public void setColor(int r, int g, int b)
    {
	this.setColor(r/255.0f, g/255.0f, b/255.0f);
    }
    public void setColor(float r, float g, float b)
    {
	GL11.glColor3f(r, g, b);
    }
    public void setColor(int r, int g, int b, int a)
    {
	this.setColor(r/255.0f, g/255.0f, b/255.0f, a/255.0f);
    }
    public void setColor(float r, float g, float b, float a)
    {
	GL11.glColor4f(r, g, b, a);
    }
    public void drawLine(int x1, int y1, int x2, int y2)
    {
	GL11.glBegin(GL11.GL_LINES);
	GL11.glVertex2i(x1, y1);
	GL11.glVertex2i(x2, y2);
	GL11.glEnd();
    }
    public void drawRect(int x, int y, int width, int height)
    {
	GL11.glBegin(GL11.GL_LINE_LOOP);
    	GL11.glVertex2i(x, y);
    	GL11.glVertex2i(x+width, y);
    	GL11.glVertex2i(x+width, y-height);
    	GL11.glVertex2i(x, y-height);
	GL11.glEnd();
    }
    public void fillRect(int x, int y, int width, int height)
    {
	GL11.glBegin(GL11.GL_QUADS);
	GL11.glVertex2i(x, y);
	GL11.glVertex2i(x+width, y);
	GL11.glVertex2i(x+width, y-height);
	GL11.glVertex2i(x, y-height);
	GL11.glEnd();
    }
    public void drawCircle(int cX, int cY, double radius)
    {
	GL11.glBegin(GL11.GL_LINE_LOOP);
	double step = 6.283185307179586d/radius;
	for(double theta = 0; theta<6.283185307179586d; theta+=step)
	{
	    GL11.glVertex2d(cX+(Math.cos(theta)*radius), cY+(Math.sin(theta)*radius));
	}
	GL11.glVertex2d(cX+radius, cY);
	GL11.glEnd();
    }
    public void fillCircle(int cX, int cY, double radius)
    {
	GL11.glBegin(GL11.GL_TRIANGLE_FAN);
	GL11.glVertex2d(cX, cY);
	double step = 6.283185307179586d/radius;
	for(double theta = 0; theta<6.283185307179586d; theta+=step)
	{
	    GL11.glVertex2d(cX+(Math.cos(theta)*radius), cY+(Math.sin(theta)*radius));
	}
	GL11.glVertex2d(cX+radius, cY);
	GL11.glEnd();
    }
    public void drawImage(Texture img, int x, int y)
    {
	this.drawImage(img, x, y, img.getImageWidth(), img.getImageHeight());
    }
    public void drawImage(Texture img, int x, int y, int w, int h)
    {
	GL11.glEnable (GL11.GL_BLEND);
	GL11.glEnable(GL11.GL_TEXTURE_2D);
	img.bind();
	GL11.glBegin(GL11.GL_QUADS);
	
		GL11.glTexCoord2f(0,0);
		GL11.glVertex2i(x, y);
		
		GL11.glTexCoord2f(1,0);
		GL11.glVertex2i(x+w, y);

		GL11.glTexCoord2f(1,1);
		GL11.glVertex2i(x+w, y+h);
		
		GL11.glTexCoord2f(0,1);
		GL11.glVertex2i(x, y+h);
	GL11.glEnd();
	GL11.glDisable(GL11.GL_TEXTURE_2D);
	GL11.glDisable(GL11.GL_BLEND);
    }
}
